package com.lsh.controller;

import com.google.code.kaptcha.Producer;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Encoder;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * capthcha验证码校验
 * @author Administrator
 * @date 2019/09/29 14:59
 */
@RestController
@RequestMapping("/captcha")
public class CaptchaController {

    @Resource
    RedisTemplate redisTemplate;

    @Resource(name = "captchaProducer")
    private Producer captchaProducer;

    @Resource(name = "captchaProducer2")
    private Producer captchaProducer2;

    /**
     * 登录
     * 获取验证码图片
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/getCaptchaCode")
    public String getCaptchaCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String capText = captchaProducer.createText(); // 生成验证码文本
        System.out.println("生成验证码文本====" + capText);
        // 利用生成的字符串构建图片
        BufferedImage bi = captchaProducer.createImage(capText);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ImageIO.write(bi, "jpg", bos);
        BASE64Encoder encoder = new BASE64Encoder();
        String base64Img = encoder.encode(bos.toByteArray());

        response.setContentType("application/json");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Controll", "no-cache");
        response.setCharacterEncoding("UTF-8");

        String token = RandomStringUtils.randomAlphanumeric(25); //生成指定长度的字成母和数字组的字符串
        while(!StringUtils.isEmpty((String) redisTemplate.opsForValue().get(token))){
            token = RandomStringUtils.randomAlphanumeric(25);
        }
        System.out.println("CTOKEN===" + token);
        redisTemplate.opsForValue().set(token, capText, 1*30, TimeUnit.SECONDS); // 当redis无该token时，则将该token的验证码存入
        Cookie captchaCookie = new Cookie("CTOKEN", token);
        captchaCookie.setMaxAge(1*30);
//        captchaCookie.setPath("/login");
        captchaCookie.setPath("/loginnew/index");
        response.addCookie(captchaCookie);
        return base64Img;
    }

    /**
     * 注册
     * 获取验证码图片
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @GetMapping("/getCaptchaCode2")
    public String getCaptchaCode2(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String capText = captchaProducer2.createText(); // 生成验证码文本
        // 利用生成的字符串构建图片
        BufferedImage bi = captchaProducer2.createImage(capText);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ImageIO.write(bi, "jpg", bos);
        BASE64Encoder encoder = new BASE64Encoder();
        String base64Img = encoder.encode(bos.toByteArray());

        response.setContentType("application/json");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Controll", "no-cache");
        response.setCharacterEncoding("UTF-8");

        String token = RandomStringUtils.randomAlphanumeric(25); //生成指定长度的字成母和数字组的字符串
        while(!StringUtils.isEmpty((String) redisTemplate.opsForValue().get(token))){
            token = RandomStringUtils.randomAlphanumeric(25);
        }
        redisTemplate.opsForValue().set(token, capText, 1*60, TimeUnit.SECONDS); // 当redis无该token时，则将该token的验证码存入
        Cookie captchaCookie = new Cookie("ETOKEN", token);
        captchaCookie.setMaxAge(1*60);
        captchaCookie.setPath("/registernew/index");
        response.addCookie(captchaCookie);
        return base64Img;
    }
}
